package com.registtools.service.impl;

import com.registtools.client.TargetURL;
import com.registtools.dao.course.CourseCurriculumDao;
import com.registtools.dao.regist.RegistStudentDao;
import com.registtools.service.ChangeCouresService;
import com.registtools.util.JsonUtil;
import com.registtools.util.RestTemplateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class ChangeCouresServiceImpl implements ChangeCouresService {
    /**
     * 缴费->调课->退费场景流程回归
     * */
    @Autowired
    CourseCurriculumDao courseCurriculumDao;
    @Autowired
    RegistStudentDao registStudentDao;
    //步骤响应结果回写
    Map<String,Object> stepsMap = new HashMap<>();


    @Override
    public String changeCourseCheck(String studentid,String classid) {
        try {
            //缴费逻辑
            MultiValueMap<String, Object> params= new LinkedMultiValueMap<String, Object>();
            params.add("citycode","010");
            params.add("studentid",studentid);
            params.add("classid",classid);
            params.add("status",0);
            params.add("workcode","270105");
            params.add("creator","崔中原");
            Map resqStep1 = RestTemplateUtil.post(TargetURL.PAY,params, Map.class).getBody();
            System.out.println(resqStep1);
            stepsMap.put("step1",resqStep1);
            Thread.sleep(1500);
            //调课
            changeCourse(studentid,classid);
            //退费
            toRefund(studentid);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //退费流程
        return JsonUtil.mapToJson(stepsMap);
    }
    /**
     * 调课逻辑
     * */
    public Map changeCourse(String studentid,String classid){
        //调课流程
        MultiValueMap<String, Object> changeCourse= new LinkedMultiValueMap<String, Object>();
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("orgCode","010");
        httpHeaders.add("clientPlatform","3");
        httpHeaders.add("clientType","0");
        changeCourse.add("employeeNo","270105");
        changeCourse.add("sourceClassOrgCode","010");
        changeCourse.add("targetClassId","b79a8138eec94af79819712030f780f5");
        changeCourse.add("sourceCurriculumId",courseCurriculumDao.getCurriculumId(classid).getId());
        changeCourse.add("registId",registStudentDao.getStudentId(studentid).getId());
        changeCourse.add("studentUid",registStudentDao.getStudentId(studentid).getStudent_uid());
        Map chageCourseResqStep2 = RestTemplateUtil.post(TargetURL.CHANGECOURSE,httpHeaders,changeCourse,Map.class).getBody();
        System.out.println(chageCourseResqStep2);
        stepsMap.put("step2",chageCourseResqStep2);
        return stepsMap;
    }
    /**
     * 退费逻辑
     * */
    public Map toRefund(String studentid){
        Map<String,Object> refundMap = new HashMap<>();
        Map<String,Object> step3Map = new HashMap<>();
        List registIdList = new ArrayList();
        registIdList.add(registStudentDao.getStudentId(studentid).getId());
        refundMap.put("refundReason","001004");
        refundMap.put("areaCode","010");
        refundMap.put("registId",registIdList);
        refundMap.put("hoursNum","0");
        refundMap.put("noNeedDeductMaterials","0");
        refundMap.put("stuNo","214010862087");
        step3Map.put("step3",refundMap);
        Map refunRespMap = RestTemplateUtil.post(TargetURL.REFUNDURL,refundMap,Map.class).getBody();
        System.out.println(refunRespMap);
        stepsMap.put("step3",refunRespMap);
        return stepsMap;
    }



}
